import axios from 'axios'
import qs from 'qs'
import { getStore, removeStore } from './../utils/local'
import { MessageBox, Message } from 'element-ui'
const instance = axios.create({
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    },
    transformRequest: [function (data) {
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }],
    baseURL: process.env.API_ROOT,
    timeout: 5000,
})


instance.interceptors.request.use(
    config => {
        return config
    },
    err => Promise.reject(err)
)

instance.interceptors.response.use(
    res => res,
    err => Promise.reject(err)
)

export function get(url, params) {
    if (getStore('authToken')) {
        instance.defaults.headers.common['accessToken'] = getStore('authToken')
    }
    return new Promise((resolve, reject) => {
        var Url = ''
        if (params === null || params === undefined) {
            Url = url
        } else {
            Url = url + '?' + qs.stringify(params)
        }
        instance.get(Url)
            .then(res => { resolve(res.data) },
                err => { 
                    if (err.response && err.response.status === 500) {
                        MessageBox.alert(`${err.response.data.message}`, '提示', {
                            confirmButtonText: '确定',
                            callback: () => {
                                removeStore('authToken')
                                window.location = `${window.location.origin}`
                            }
                        })
                    } else {
                        Message.error('网络错误')
                    }
                }).catch(() => {
                    Message.error('网络错误')
                })
    })
}
export function post(url, params) {
    if (getStore('authToken')) {
        instance.defaults.headers.common['accessToken'] = getStore('authToken')
    }
    return new Promise((resolve, reject) => {
        instance.post(url, params)
            .then(res => {
                resolve(res.data)
            },
                err => {
                    if (err.response && err.response.status === 500) {
                        MessageBox.alert(`${err.response.data.message}`, '提示', {
                            confirmButtonText: '确定',
                            callback: () => {
                                removeStore('authToken')
                                window.location = `${window.location.origin}`
                            }
                        })
                    } else {
                        Message.error('网络错误')
                    }
                }).catch(() => {
                    Message.error('网络错误')
                })
    })
}

export function del(url, params) {
    return new Promise((resolve, reject) => {
        instance.post(url, params)
            .then(res => {
                resolve(res.data)
            },
                err => { 
                    if (err.response.status === 500) {
                        MessageBox.alert(`${err.response.data.message}`, '提示', {
                            confirmButtonText: '确定',
                            callback: () => {
                                removeStore('authToken')
                                window.location = `${window.location.origin}`
                            }
                        })
                    } else {
                        Message.error(err && err.response.data.message)
                    }
                })
    })
}

export function upload(params, success) {
    axios({
        method: 'POST',
        baseURL: 'http://www.hbzhiku.org.cn/img_ts_upload/',
        headers: {'Content-Type': 'multipart/form-data'},
        data: params,
    }).then(res => {
        success(res.data)
    }).catch(function (response) {
       Message.error(response.message)
    });
}


export function uploadAddressBook(params, success) {
    axios({
        method: 'POST',
        baseURL: 'http://www.yuwubao.cn:8780/ts/bk/importUserList',
        headers: {'Content-Type': 'multipart/form-data'},
        data: params,
    }).then(res => {
        success(res.data)
    }).catch(function (response) {
       Message.error(response.message)
    });
}


export function download(params, success) {
    axios({
        method: 'GET',
        baseURL: 'http://www.yuwubao.cn:8780/ts-upload/tsui/downloadZipFile' + '?' + qs.stringify(params),
    }).then(res => {
        success(res.data)
    }).catch(res => {
        Message.error(res.message)
    })
}
